package com.q.tqb.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.q.tqb.R;
import com.q.tqb.activity.mine.setting.SettingBindTelActivity;
import com.q.tqb.activity.mine.wallet.WalletMoneyDetailActivity;
import com.q.tqb.activity.mine.wallet.WalletWithdrawActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.BalanceInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MineWalletActivity extends BaseActivity {


    @BindView(R.id.tv_mine_money)
    TextView mTvMineMoney;
    @BindView(R.id.tv_pay)
    TextView mTvPay;
    @BindView(R.id.tv_tip1)
    TextView tv_tip1;
    @BindView(R.id.tv_tip2)
    TextView tv_tip2;
    @BindView(R.id.tv_freeze)
    TextView tv_freeze;
    @BindView(R.id.tv_cash)
    TextView tv_cash;
    @BindView(R.id.tv_integral)
    TextView tv_integral;
    private String money;
    private int uid;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_wallet;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle(getString(R.string.mine_wallet));
        setTvRight("明细");
        uid = (int) SPUtil.get(this, "uid", 0);
        money = SPUtil.getString(this, "withdraw");
        mTvMineMoney.setText(String.format("%s%s", getString(R.string.string_money_sign), money));
        getMoney();
    }

    private void getMoney() {

        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("token", StringUtil.getToken(uid));
        mProgressDialog.show();
        OkHttpUtil.getInstance().post(this, AppConst.URL_GET_MONEY, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                mProgressDialog.dismiss();
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                BalanceInfo balanceInfo = gson.fromJson(result, BalanceInfo.class);
                money = balanceInfo.getBalance();

                SPUtil.put(MineWalletActivity.this, "withdraw", money);
                mTvMineMoney.setText(String.format("%s%s", getString(R.string.string_money_sign), money));

                tv_tip1.setText(balanceInfo.tip1);
                tv_tip2.setText(balanceInfo.tip2);

                tv_cash.setText(balanceInfo.getCash());
                tv_integral.setText(balanceInfo.getIntegral());
                double ss = Double.parseDouble(balanceInfo.getBalance_freeze());
                if (ss <= 0) {
                    tv_freeze.setVisibility(View.INVISIBLE);
                }else{
                    tv_freeze.setVisibility(View.VISIBLE);
                    tv_freeze.setText(String.format(getString(R.string.txt_freeze),balanceInfo.getBalance_freeze()));
                }
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                mProgressDialog.dismiss();
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_pay, R.id.tv_right, R.id.tv_my_card, R.id.tv_xjb})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                intent = new Intent(MineWalletActivity.this, WalletMoneyDetailActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_pay:
                intent = new Intent(MineWalletActivity.this, WalletWithdrawActivity.class);
                startActivityForResult(intent, 50);
                break;
            case R.id.tv_my_card:
                intent = new Intent(MineWalletActivity.this, MineCardActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_xjb:
                intent = new Intent(MineWalletActivity.this, BuyCoinsActivity.class);
                startActivityForResult(intent, 50);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 50 && resultCode == RESULT_OK) {
            getMoney();
        }
    }
}
